package com.shushan.home.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shushan.base.BaseActivity;
import com.shushan.exam.OnResultCollectListener;
import com.shushan.exam.adapter.VideoPointsAdapter;
import com.shushan.exam.bean.ExerciseBean;
import com.shushan.exam.bean.ExerciseOptionBean;
import com.shushan.exam.bean.ExerciseVideoBean;
import com.shushan.exam.bean.PaperBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName: ExamScoreAdapter
 * @Desciption: 练习记录详情考试成绩adapter
 * @author: zhangshihao
 * @date: 2018-04-25
 */
public class ExerciseRecordScoreAdapter extends RecyclerView.Adapter<ExerciseRecordScoreAdapter.ViewHolder> {

    private static final String TAG = ExerciseRecordScoreAdapter.class.getSimpleName();

    private BaseActivity context;
    private PaperBean examPaperBean;
    private OnResultCollectListener onResultCollectListener;
    private List<ExerciseBean> exerciseBeanlist = new ArrayList<ExerciseBean>();

    public ExerciseRecordScoreAdapter(BaseActivity context, PaperBean examPaperBean) {
        this.context = context;
        this.examPaperBean = examPaperBean;

        if(examPaperBean != null){
            List<ExerciseBean> chineseExerciseList = examPaperBean.getChineseExerciseList();
            for (int i = 0;i < chineseExerciseList.size();i++){
                ExerciseBean exerciseBean = chineseExerciseList.get(i);
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                if(isExamWrong(optionList,exerciseBean.my_answer_id)){
                    exerciseBeanlist.add(exerciseBean);
                }
            }

            List<ExerciseBean> mathExerciseList = examPaperBean.getMathExerciseList();
            for (int i = 0;i < mathExerciseList.size();i++){
                ExerciseBean exerciseBean = mathExerciseList.get(i);
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                if(isExamWrong(optionList,exerciseBean.my_answer_id)){
                    exerciseBeanlist.add(exerciseBean);
                }
            }

            List<ExerciseBean> englishExerciseList = examPaperBean.getEnglishExerciseList();
            for (int i = 0;i < englishExerciseList.size();i++){
                ExerciseBean exerciseBean = englishExerciseList.get(i);
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                if(isExamWrong(optionList,exerciseBean.my_answer_id)){
                    exerciseBeanlist.add(exerciseBean);
                }
            }
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.exercise_record_score_list_item, null,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ExerciseBean exerciseBean = exerciseBeanlist.get(position);
        if(exerciseBean.child_collect == 1){
            holder.collectImg.setImageResource(R.drawable.ic_score_collected);
        }else {
            holder.collectImg.setImageResource(R.drawable.ic_score_uncollected);
        }

        holder.collectImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealCollect(position);
            }
        });

        holder.numberTextView.setText("第 " + (position+1) + " 题");
        if(exerciseBean.content_type.equals("TEXT")){
            holder.questionText.setVisibility(View.VISIBLE);
            holder.questionImg.setVisibility(View.GONE);

            holder.questionText.setText(exerciseBean.exercises_content);
        }else {
            holder.questionText.setVisibility(View.GONE);
            holder.questionImg.setVisibility(View.VISIBLE);

            ImageUtils.loadImageNoBg(context,exerciseBean.exercises_imgs,holder.questionImg);
        }
        ExerciseRecordAnswerAdapter answerAdapter = new ExerciseRecordAnswerAdapter(context,exerciseBean);
        holder.answerRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        holder.answerRecyclerView.setAdapter(answerAdapter);

        List<ExerciseVideoBean> videoPointList = exerciseBean.getVideoList();
        if(videoPointList != null && videoPointList.size() > 0){
            holder.pointsLayout.setVisibility(View.VISIBLE);

            holder.videoListView.setAdapter(new VideoPointsAdapter(context,videoPointList));
        }else {
            holder.pointsLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return exerciseBeanlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView numberTextView;
        ImageView collectImg;
        TextView questionText;
        ImageView questionImg;
        RecyclerView answerRecyclerView;
        LinearLayout pointsLayout;
        ListView videoListView;
        public ViewHolder(View itemView) {
            super(itemView);
            //通过ID查询控件
            numberTextView = itemView.findViewById(R.id.exam_score_no_tv);
            collectImg = itemView.findViewById(R.id.exam_score_collect_img);
            questionText = itemView.findViewById(R.id.exam_score_question_tv);
            questionImg = itemView.findViewById(R.id.exam_score_question_img);
            answerRecyclerView = itemView.findViewById(R.id.exam_score_answer_lv);
            pointsLayout = itemView.findViewById(R.id.exam_score_video_layout);
            videoListView = itemView.findViewById(R.id.exam_score_video_lv);
        }
    }


    private boolean isExamWrong(List<ExerciseOptionBean> optionList,int answerId) {
        if(optionList == null || optionList.size() == 0){
            return false;
        }

        for(ExerciseOptionBean optionBean : optionList){
            if(optionBean.id == answerId && optionBean.option_result == 0){
                return true;
            }
        }
        return false;
    }

    public OnResultCollectListener getOnResultCollectListener() {
        return onResultCollectListener;
    }

    public void setOnResultCollectListener(OnResultCollectListener onResultCollectListener) {
        this.onResultCollectListener = onResultCollectListener;
    }

    private void dealCollect(int position) {
        if(onResultCollectListener == null){
            return;
        }

        ExerciseBean exerciseBean = exerciseBeanlist.get(position);

        onResultCollectListener.onResultCollect(exerciseBean);
    }
}
